package cn.herlioner.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import cn.herlioner.dao.impl.AdministratorDAOImpl;
import cn.herlioner.pojo.Administrator;
import cn.herlioner.service.PageService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AdministratorAction extends ActionSupport implements SessionAware{
	Administrator administrator;
	AdministratorDAOImpl administratorDAO = new AdministratorDAOImpl();
	List<Administrator> administrators = new ArrayList<>();
	private HttpServletRequest request;
	private PageService pageService;
	private int currentPagechange;//当前�?
	/**
	 * 获取�?��的管理账户列�?
	 * @return
	 */
	public String list(){
		int pageSize = 1 ;
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if(request.getParameter("currentPagechange")==null){
			currentPagechange = (int)session.get("currentPagechange");
			System.out.println(currentPagechange);
		}else{
			currentPagechange = Integer.valueOf(request.getParameter("currentPagechange"));
		}
		
		PageService Pageservice = administratorDAO.PageSelect(currentPagechange, pageSize);
			try{
				for(int i=0;i<pageSize;i++){
					Administrator a = (Administrator)Pageservice.getRecordList().get(i);
					administrators.add(a);
					System.out.println(administrators.get(i).getAdname());
				}
			}catch (Exception e) {
				// TODO: handle exception
			}
		request.setAttribute("administrators", administrators);
		request.setAttribute("PageService", Pageservice);
		return "success";
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public int getCurrentPagechange() {
		return currentPagechange;
	}
	public void setCurrentPagechange(int currentPagechange) {
		this.currentPagechange = currentPagechange;
	}
	/**
	 * 删除某一项元�?
	 * @return
	 */
	public String delete(){
		Map session = ActionContext.getContext().getSession();
		//request域中获取administrator值删除相应的�?
		administratorDAO.delete(administrator.getAdministrator());
		//removesession中原来的administrators值，用更新后的administrators取代
		session.remove("administrators");
		administrators = administratorDAO.list();
		session.put("administrators", administrators);
		return "success";
	}
	/**
	 * 添加administrator元素
	 * @return
	 */
	public String add(){
		Map session = ActionContext.getContext().getSession();
		administratorDAO.add(administrator);
		if(session.get("administrators")!=null){
			session.remove("administrators");
			administrators = administratorDAO.list();
			session.put("administrators", administrators);
			return "success";
		}else{
			administrators = administratorDAO.list();
			session.put("administrators", administrators);
			return "success";
		}
	}
	/**
	 * 得到某一个administrator元素
	 * @return
	 */
	public String get(){
		administrator = administratorDAO.get(administrator.getAdministrator());
		Map session = ActionContext.getContext().getSession();
		session.put("administrator", administrator);
		return "success";
	}
	/**
	 * 更新某一administrator元素
	 * @return
	 */
	public String update(){
		Map session = ActionContext.getContext().getSession();
		Administrator mapadministrator = (Administrator)session.get("administrator");
		System.out.println(mapadministrator.toString());
		//如果只更新一项就�?��给其赋�?
		if(administrator.getAdministrator()==0){
			administrator.setAdministrator(mapadministrator.getAdministrator());
		}
		if(administrator.getAdname().equals("")||administrator.getAdname()==null){
			System.out.println(mapadministrator.getAdname());
			administrator.setAdname(mapadministrator.getAdname());
		}
		if(administrator.getAdpad().equals("")||administrator.getAdpad()==null){
			System.out.println(mapadministrator.getAdpad());
			administrator.setAdpad(mapadministrator.getAdpad());
		}
		System.out.println(administrator.toString());
		administratorDAO.update(administrator);
		session.remove("administrators");
		administrators = administratorDAO.list();
		session.put("administrators", administrators);
		return "success";
	}
	
	
	public PageService getPageService() {
		return pageService;
	}
	public void setPageService(PageService pageService) {
		this.pageService = pageService;
	}
	public void setServletRequest(HttpServletRequest arg0) {
	        this.request = arg0;
	 }
	
	public Administrator getAdministrator() {
		return administrator;
	}

	public void setAdministrator(Administrator administrator) {
		this.administrator = administrator;
	}

	public AdministratorDAOImpl getAdministratorDAO() {
		return administratorDAO;
	}

	public void setAdministratorDAO(AdministratorDAOImpl administratorDAO) {
		this.administratorDAO = administratorDAO;
	}

	public List<Administrator> getAdministrators() {
		return administrators;
	}

	public void setAdministrators(List<Administrator> administrators) {
		this.administrators = administrators;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

}
